package com.initialize.pub.util;

import com.initialize.pub.constantutil.OssTUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.ListObjectsRequest;
import com.qcloud.cos.model.ObjectListing;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jiandan-figure-bed
 * @description: 腾讯云OSS对象存储
 * @author: Mr.Jane
 * @create: 2019-10-12 11:52
 **/
public class OssTencentUtil {

    /**
     * 创建一个腾讯对象存储的客户端
     *
     * @return
     */
    public static COSClient getCOSClient() {
        COSCredentials cred = new BasicCOSCredentials(OssTUtil.SECRET_ID, OssTUtil.SECRET_KEY);
        ClientConfig clientConfig = new ClientConfig(
                new Region(OssTUtil.REGION_NAME)
        );
        return new COSClient(cred, clientConfig);
    }

    /**
     * @return 获取文件列表
     * @throws CosClientException
     * @throws CosServiceException
     */
    public Map<String, Object> listObjects(String Prefix) throws CosClientException, CosServiceException {
        // Bucket的命名格式为 BucketName-APPID ，此处填写的存储桶名称必须为此格式

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        // 设置bucket名称
        listObjectsRequest.setBucketName(OssTUtil.BUCKET_NAME);
        // prefix表示列出的object的key以prefix开始
        listObjectsRequest.setPrefix(Prefix);
        // deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object
        listObjectsRequest.setDelimiter("/");
        // 设置最大遍历出多少个对象, 一次listobject最大支持1000
        listObjectsRequest.setMaxKeys(1000);
        ObjectListing objectListing = null;

        COSClient cosClient = OssTencentUtil.getCOSClient();
        try {
            objectListing = cosClient.listObjects(listObjectsRequest);
        } catch (CosClientException e) {
            e.printStackTrace();
            return null;
        }
        //处理需要的信息
        Map<String, Object> fileInformationMap = new LinkedHashMap<>();
        //当前目录的文件夹和文件
        List<COSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
        List<String> commonPrefixes = objectListing.getCommonPrefixes();
        //下级目录
        fileInformationMap.put("lowerDirectory", commonPrefixes);

        Map<String, Object> theLowerFileMap = new LinkedHashMap<>();
        if (objectSummaries.size() == 1) {
            System.err.println("没有文件");
        } else {
            for (int i = 0; i < objectSummaries.size(); i++) {
                COSObjectSummary objectSummary = objectSummaries.get(i);

                String key = objectSummary.getKey();
                long fileSize = objectSummary.getSize();
                // 文件的路径key
                String theFileUrl = "https://" + OssTUtil.BUCKET_NAME + ".cos.ap-chengdu.myqcloud.com/" + key;
                theLowerFileMap.put("theFileUrl", theFileUrl);
                theLowerFileMap.put("fileSize", fileSize / 1024 + "KB");
            }
        }
        fileInformationMap.put("theLowerFileMap", theLowerFileMap);

        return fileInformationMap;
    }

    /**
     * 上传文件到腾讯的存储桶
     * 并且删除本地的文件
     *
     * @param cosFile
     * @return
     */
    public static String picCOS(File cosFile) {
        String name = cosFile.getName();
        COSClient cosClient = OssTencentUtil.getCOSClient();
        PutObjectRequest putObjectRequest = new PutObjectRequest(OssTUtil.BUCKET_NAME, "img/" + name, cosFile);
        cosClient.putObject(putObjectRequest);
        String key = putObjectRequest.getKey();
        cosFile.delete();
        // 文件的路径key
        return "https://" + OssTUtil.BUCKET_NAME + ".cos.ap-shanghai.myqcloud.com/" + key;

    }

}